package com.oet.web.freemarker;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;



import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class HomepageGenerator implements Controller

{
	private static Logger log = Logger.getLogger(HomepageGenerator.class);
//	private YdkSimWebFrontEndService ydkSimFrontEndService;
	
	private static final String regexString = "doImage.do[?]fileName=book[\\]";
	private static final Pattern regex = Pattern.compile("doImage.do.*?fileName=book\\\\");
	
	//Delete the String ";jsessionid=5A404C5CBA6F6F80A59350DF557D9F4D?"
	private static final Pattern regexDeleteJSession = Pattern.compile("[;]jsessionid=[0-9a-zA-Z]*");
	
	
	private static final String target = "allfiles/book/";

//	public YdkSimWebFrontEndService getYdkSimFrontEndService() {
//		return ydkSimFrontEndService;
//	}
//
//	public void setYdkSimFrontEndService(
//			YdkSimWebFrontEndService ydkSimFrontEndService) {
//		this.ydkSimFrontEndService = ydkSimFrontEndService;
//	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		generateYdkHtml();
		return null;
		
	}
	
	public static void generateYdkHtml()
	{
		URL url = null;
		try {
			
			url = new URL("http://www.yueduke.com:8088/YdkWeb/homepage.do?errorMsg=generateHtml");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		URL url = createURL("http://www.baidu.com", null);
		if(url==null)
		{
			log.warn("url format is wrong");
			return;
		}
//		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		HttpURLConnection connection;
		try {
			connection = (HttpURLConnection)url.openConnection();
			connection.setConnectTimeout(20000);
//			connection.
			InputStreamReader reader = new InputStreamReader(connection.getInputStream(),"UTF-8");            
			
			
//			InputStreamReader reader = new InputStreamReader(url.openStream(),"UTF-8");
	        //System.out.println("content: "+connection.getContent().toString());
			BufferedReader br = new BufferedReader(reader);
			String result = br.readLine();
			
			String htmlFilePath = Function.getPath() + "\\ydk.html";
			log.info("模板路径＝"+htmlFilePath);
			
			File afile = new File(htmlFilePath);
			Writer out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(afile), "utf-8"));
			for (;result != null;result = br.readLine())
			{

				Matcher m = regexDeleteJSession.matcher(result);
				if (m!= null)
				{
//					result = m.replaceAll("?");
					result = m.replaceAll("");
				}
//				result.replaceAll(regexString, target);
//				Matcher m = regex.matcher(result);
//				if (m!= null)
//				{
//					result = m.replaceAll(target);
//				}
//				if (m.matches())
//				{
//					m.replaceAll((target));
//				}
				out.write(result);
			}
			out.flush();
			out.close();
			br.close();
			reader.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
